<?php 
//1.获取必要的参数
//1.1获取分类的ID
$categoryId = $_POST["categoryId"];
//1.2
$currentPage = $_POST["currentPage"];
//1.3
$pageSize = $_POST["pageSize"];
//计算出要从哪里开始获取
$offset = ($currentPage - 1) * $pageSize;
//2.查询数据库
//2.1 连接数据库
 $connect = connect();
 //2.2 sql语句
 $sql = "SELECT p.id,p.title,p.created,p.content,p.views,p.likes,p.feature,u.nickname,c.`name`,
(select count(id) FROM comments WHERE comments.post_id = p.id) as commentCount
FROM posts p
LEFT JOIN users u ON u.id = p.user_id
LEFT JOIN categories c on c.id = p.category_id
WHERE p.category_id = {$categoryId}
LIMIT {$offset},{$pageSize}";
//2.3执行差选
$postArr = query($connect,$sql);
$response = ["code"=>0,"msg"=>"操作失败"];
//3.返回数据
if($postArr){
	$response["code"] = 1;
	$response["msg"] = "操作成功";
	$response["data"] = $postArr;
} 
//以json格式返回数据
header("content-type: application/json;charset=utf-8");
echo json_encode($response);
 ?>


